#define _CRT_SECURE_NO_WARNINGS 1
#include"Stack.h"

void STInit(ST* st)
{
	assert(st);
	st->a = NULL;
	st->capacity = st->top = 0;
}

void STDestroy(ST* st)
{
	assert(st);
	free(st->a);
	st->capacity = st->top = 0;
}

void STPush(ST* st, STDataType x)
{
	assert(st);
	if (st->top == st->capacity)
	{
		int newcapacity = st->capacity == 0 ? 4 : 2 * st->capacity;
		STDataType* tmp = (STDataType*)realloc(st->a, sizeof(STDataType) * newcapacity);
		if (tmp == NULL)
		{
			perror("realloc fail!");
			return;
		}
		st->a = tmp;
		st->capacity = newcapacity;
	}
	st->a[st->top] = x;
	st->top++;
}

void STPop(ST* st)
{
	assert(st);
	assert(st->top > 0);
	st->top--;
}

STDataType STTop(ST* st)
{
	assert(st);
	return st->a[st->top - 1];
}

bool STEmpty(ST* st)
{
	assert(st);
	return st->top == 0;
}

int STSize(ST* st)
{
	assert(st);
	return st->top;
}